package com.mc.read.adminapi.manager;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mc.read.common.emun.ServiceCodeEnum;
import com.mc.read.common.exception.ServiceException;
import com.mc.read.dao.mapper.UserinformationMapper;
import com.mc.read.dao.mapper.UserinformationdetMapper;
import com.mc.read.dao.models.Userinformation;
import com.mc.read.dao.models.UserinformationdetCustom;
import com.mc.read.dao.vo.PageResposeVo;
import com.mc.read.dao.vo.PageVo;
import com.mc.read.vo.request.BaseRequest;
import com.mc.read.vo.respose.BaseRespose;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author liuhao
 * @date 2019/6/15 11:58
 */
@Component
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
public class PcUserManager {
    @Autowired
    UserinformationdetMapper userinformationdetMapper;
    @Autowired
    UserinformationMapper userinformationMapper;

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 用户列表展示 selectPcUserList
     **/
    public BaseRespose selectPcUserList(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();

        PageVo pageVo = null;
        try {
            pageVo = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), PageVo.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        PageHelper.startPage(pageVo.getPagenum(), pageVo.getPagesize());
        PageHelper.orderBy("ui.createdate desc");
        List<UserinformationdetCustom> list = userinformationdetMapper.selectPcUserList(pageVo);
        PageInfo<UserinformationdetCustom> pageInfo = new PageInfo<>(list);
        PageResposeVo pageResposeVo = new PageResposeVo(pageInfo.getTotal(), pageInfo.getList());
        baseRespose.setReturnString(pageResposeVo);
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 修改用户禁用状态 updateUserStaus
     **/
    public BaseRespose updateUserStaus(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();

        Userinformation userinformation = null;
        try {
            userinformation = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), Userinformation.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        if (ObjectUtils.isEmpty(userinformation.getUserid()) || ObjectUtils.isEmpty(userinformation.getStaus())) {
            throw new ServiceException(203020, "请求信息不完整", JSON.toJSONString(baseRequest));
        }
        Userinformation userinformation1=new Userinformation();
        userinformation1.setUserid(userinformation.getUserid());
        userinformation1.setStaus(userinformation.getStaus());
        userinformationMapper.updateByPrimaryKeySelective(userinformation1);
        baseRespose.setReturnString(" ");

        return baseRespose;
    }


}
